package com.sen.gulimall.cart.vo;

import org.apache.commons.collections4.CollectionUtils;

import java.math.BigDecimal;
import java.util.List;

/**
 * 整个购物车
 *
 * @author Kisen
 * @email lqs0911@qq.com
 * @date 2022/11/6 17:41
 * @detail 需要计算的属性，必须重写它的get方法，保证每次获取属性都会进行计算
 */
public class Cart {

    private List<CartItem> items; //购物车子项信息
    private Integer countNum; //商品数量
    private Integer countType; //商品类型数量
    private BigDecimal totalAmount; //商品总价
    private BigDecimal reduce = BigDecimal.ZERO; //减免价格

    public List<CartItem> getItems() {
        return items;
    }

    public void setItems(List<CartItem> items) {
        this.items = items;
    }

    public Integer getCountNum() {
        int count = 0;
        if (CollectionUtils.isNotEmpty(this.items)) {
            count = this.items.stream().mapToInt(CartItem::getCount).sum();
        }
        return count;
    }

    public Integer getCountType() {
        int count = 0;
        if (CollectionUtils.isNotEmpty(this.items)) {
            count = this.items.size();
        }
        return count;
    }

    public BigDecimal getTotalAmount() {
        BigDecimal amount = BigDecimal.ZERO;
        //1、计算购物项总价
        if (CollectionUtils.isNotEmpty(this.items)) {
            for (CartItem cartItem : this.items) {
                if (cartItem.getCheck()) {
                    amount = amount.add(cartItem.getTotalPrice());
                }
            }
        }

        //2、减去优惠总价
        return amount.subtract(getReduce());
    }

    public BigDecimal getReduce() {
        return reduce;
    }

    public void setReduce(BigDecimal reduce) {
        this.reduce = reduce;
    }
}
